#!/bin/bash

# 简化部署脚本 - 使用expect自动化

SERVER_IP="212.64.32.91"
SERVER_PORT="22"
SERVER_USER="ubuntu"
SERVER_PASSWORD="shafei@0607"
REMOTE_DIR="/home/ubuntu/jd_credit"
REMOTE_LOG_DIR="/home/ubuntu/jd_credit/logs"
LOCAL_JAR="target/jd_credit-1.0-SNAPSHOT.jar"

echo "🚀 开始部署JD信贷系统到云服务器..."
echo "服务器: $SERVER_USER@$SERVER_IP"
echo ""

# 步骤1: 打包
echo "📦 [1/3] 打包JAR文件..."
mvn clean package -DskipTests -q

if [ ! -f "$LOCAL_JAR" ]; then
    echo "❌ 打包失败"
    exit 1
fi
echo "✅ 打包完成"
echo ""

# 步骤2: 上传
echo "📤 [2/3] 上传到服务器..."

# 检查是否安装expect
if ! command -v expect &> /dev/null; then
    echo "⚠️  expect未安装，尝试使用sshpass..."
    
    if ! command -v sshpass &> /dev/null; then
        echo "❌ 请安装sshpass或expect"
        echo "macOS: brew install hudochenkov/sshpass/sshpass"
        echo "Linux: sudo apt-get install sshpass"
        exit 1
    fi
    
    # 使用sshpass上传
    sshpass -p "$SERVER_PASSWORD" scp -o StrictHostKeyChecking=no -P $SERVER_PORT "$LOCAL_JAR" "$SERVER_USER@$SERVER_IP:$REMOTE_DIR/"
    UPLOAD_RESULT=$?
else
    # 使用expect上传
    expect -c "
        set timeout 30
        spawn scp -P $SERVER_PORT $LOCAL_JAR $SERVER_USER@$SERVER_IP:$REMOTE_DIR/
        expect {
            \"(yes/no)\" {send \"yes\r\"; exp_continue}
            \"password:\" {send \"$SERVER_PASSWORD\r\"}
        }
        expect eof
    " > /dev/null 2>&1
    UPLOAD_RESULT=$?
fi

if [ $UPLOAD_RESULT -ne 0 ]; then
    echo "❌ 上传失败"
    exit 1
fi
echo "✅ 上传完成"
echo ""

# 步骤3: 重启服务
echo "🔄 [3/3] 重启服务..."

if command -v expect &> /dev/null; then
    # 使用expect执行重启
    expect -c "
        set timeout 30
        spawn ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \"cd $REMOTE_DIR && chmod +x restart.sh && ./restart.sh\"
        expect {
            \"(yes/no)\" {send \"yes\r\"; exp_continue}
            \"password:\" {send \"$SERVER_PASSWORD\r\"}
        }
        expect eof
    " > /dev/null 2>&1
else
    # 使用sshpass执行重启
    sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USER@$SERVER_IP "cd $REMOTE_DIR && chmod +x restart.sh && ./restart.sh" > /dev/null 2>&1
fi

echo "✅ 重启命令已执行"
echo ""
echo "🎉 部署完成！"
echo ""

# 等待服务启动
echo "⏳ 等待服务启动（5秒）..."
sleep 5
echo ""

# 查看日志选项
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📋 是否查看服务器日志？"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "1) 实时查看日志 (tail -f)"
echo "2) 查看最新50行日志"
echo "3) 查看最新100行日志"
echo "4) 列出所有日志文件"
echo "5) 不查看，直接退出"
echo ""
read -p "请选择 [1-5]: " choice

case $choice in
    1)
        echo ""
        echo "📄 实时查看日志（按Ctrl+C退出）..."
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        
        if command -v sshpass &> /dev/null; then
            # 找到最新的日志文件并实时查看
            sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
                "cd $REMOTE_LOG_DIR && tail -f \$(ls -t *.log 2>/dev/null | head -1)"
        else
            expect -c "
                set timeout -1
                spawn ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \"cd $REMOTE_LOG_DIR && tail -f \\\$(ls -t *.log 2>/dev/null | head -1)\"
                expect {
                    \"(yes/no)\" {send \"yes\r\"; exp_continue}
                    \"password:\" {send \"$SERVER_PASSWORD\r\"}
                }
                interact
            "
        fi
        ;;
    2)
        echo ""
        echo "📄 最新50行日志："
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        
        if command -v sshpass &> /dev/null; then
            sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
                "cd $REMOTE_LOG_DIR && tail -n 50 \$(ls -t *.log 2>/dev/null | head -1)"
        else
            expect -c "
                set timeout 30
                spawn ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \"cd $REMOTE_LOG_DIR && tail -n 50 \\\$(ls -t *.log 2>/dev/null | head -1)\"
                expect {
                    \"(yes/no)\" {send \"yes\r\"; exp_continue}
                    \"password:\" {send \"$SERVER_PASSWORD\r\"}
                }
                expect eof
            "
        fi
        echo ""
        ;;
    3)
        echo ""
        echo "📄 最新100行日志："
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        
        if command -v sshpass &> /dev/null; then
            sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
                "cd $REMOTE_LOG_DIR && tail -n 100 \$(ls -t *.log 2>/dev/null | head -1)"
        else
            expect -c "
                set timeout 30
                spawn ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \"cd $REMOTE_LOG_DIR && tail -n 100 \\\$(ls -t *.log 2>/dev/null | head -1)\"
                expect {
                    \"(yes/no)\" {send \"yes\r\"; exp_continue}
                    \"password:\" {send \"$SERVER_PASSWORD\r\"}
                }
                expect eof
            "
        fi
        echo ""
        ;;
    4)
        echo ""
        echo "📁 日志文件列表："
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        
        if command -v sshpass &> /dev/null; then
            sshpass -p "$SERVER_PASSWORD" ssh -o StrictHostKeyChecking=no -p $SERVER_PORT $SERVER_USER@$SERVER_IP \
                "ls -lh $REMOTE_LOG_DIR/*.log 2>/dev/null || echo '暂无日志文件'"
        else
            expect -c "
                set timeout 30
                spawn ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \"ls -lh $REMOTE_LOG_DIR/*.log 2>/dev/null || echo '暂无日志文件'\"
                expect {
                    \"(yes/no)\" {send \"yes\r\"; exp_continue}
                    \"password:\" {send \"$SERVER_PASSWORD\r\"}
                }
                expect eof
            "
        fi
        echo ""
        ;;
    5|*)
        echo ""
        echo "✅ 部署完成，已退出"
        ;;
esac

echo ""
echo "💡 提示："
echo "   查看日志: ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP \"tail -f $REMOTE_LOG_DIR/*.log\""
echo "   登录服务器: ssh -p $SERVER_PORT $SERVER_USER@$SERVER_IP"
echo ""
